/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author oxplay
 */
public class ValueMap {

    private final static Logger logger = Logger.getLogger(ValueMap.class.getName());
    private int[] valueMap;
    public int offsetX, offsetY, offsetZ, iterationsX, iterationsY, iterationsZ;
    public Vector3i offsetGlobal;
    private Vector3i minPoint;
    private Vector3i maxPoint;
    public Vector3i minOffsetPoint;
    public Vector3i maxOffsetPoint;

    public ValueMap(Vector3i startPoint, Vector3i endPoint, Vector3i offset) {
        offsetGlobal = offset;
        this.minPoint = new Vector3i(Math.min(startPoint.x, endPoint.x), Math.min(startPoint.y, endPoint.y), Math.min(startPoint.z, endPoint.z));
        this.maxPoint = new Vector3i(Math.max(startPoint.x, endPoint.x), Math.max(startPoint.y, endPoint.y), Math.max(startPoint.z, endPoint.z));
        offsetX = minPoint.x - offsetGlobal.x;
        offsetY = minPoint.y - offsetGlobal.y;
        offsetZ = minPoint.z - offsetGlobal.z;
        iterationsX = maxPoint.x - offsetX + offsetGlobal.x;
        iterationsY = maxPoint.y - offsetY + offsetGlobal.y;
        iterationsZ = maxPoint.z - offsetZ + offsetGlobal.z;
        this.minOffsetPoint = new Vector3i(offsetX, offsetY, offsetZ);
        this.maxOffsetPoint = new Vector3i(offsetX + iterationsX, offsetY + iterationsY, offsetZ + iterationsZ);
        valueMap = new int[iterationsX * iterationsY * iterationsZ];
        logger.setLevel(Level.OFF);
        logger.log(Level.INFO, "offset [{0},{1},{2}]", new Object[]{offsetX, offsetY, offsetZ});
        logger.log(Level.INFO, "iterations [{0},{1},{2}]", new Object[]{iterationsX, iterationsY, iterationsZ});
        logger.log(Level.INFO, "minPoint: {0}", minPoint);
        logger.log(Level.INFO, "maxPoint: {0}", maxPoint);
    }
    
    public boolean isVectorInBound(int x, int y, int z) {
        if (x < 0 || y < 0 || z < 0 || x >= iterationsX || y >= iterationsY || z >= iterationsZ) {
            return false;
        }
        return true;
    }

    public void fillMoveable(Set<Vector3i> set) {
        for (Iterator<Vector3i> it = set.iterator(); it.hasNext();) {
            Vector3i vector3i = it.next();
            int[] vect = new int[]{vector3i.x - offsetX, vector3i.y - offsetY, vector3i.z - offsetZ};
            if (isVectorInBound(vect[0], vect[1], vect[2])) {
                setValue(vect[0], vect[1], vect[2], -1);
            }
        }
    }

    public int getValue(int x, int y, int z) {
        return valueMap[(x * iterationsY * iterationsZ) + (y * iterationsZ) + z];
    }

    public void setValue(int x, int y, int z, int set) {
        valueMap[(x * iterationsY * iterationsZ) + (y * iterationsZ) + z] = set;
    }

    public void fillValue(int set) {
        for (int x = 0; x < iterationsX; x++) {
            for (int z = 0; z < iterationsZ; z++) {
                for (int y = 0; y < iterationsY; y++) {
                    valueMap[(x * iterationsY * iterationsZ) + (y * iterationsZ) + z] = set;
                }
            }
        }
    }
}
